package com.example.a12769.myapplication.base.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import java.util.*

/**
 *   mList 数据集合
 *   resLayout 布局资源
 * */
abstract class BaseRVAdapter<T>(var mList: List<T>?, private var resLayout: Int, private var mContext: Context) : RecyclerView.Adapter<RVViewHolder>() {
    private var recyclerViewListener: IRecyclerViewListener? = null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RVViewHolder {
        val view = LayoutInflater.from(mContext).inflate(resLayout, parent, false)
        return RVViewHolder(mContext, view)
    }

    override fun onBindViewHolder(viewHolder: RVViewHolder, position: Int) {
        //作为一个抽象方法提供给外边：用来绑定数据
        bindAction(viewHolder, mList!![position])
        //绑定事件
        if (recyclerViewListener != null) {
            viewHolder.itemView.setOnClickListener(View.OnClickListener { recyclerViewListener!!.onItemClick(viewHolder.itemView, viewHolder.layoutPosition) })
            viewHolder.itemView.setOnLongClickListener(View.OnLongClickListener {
                recyclerViewListener!!.onItemLongClick(viewHolder.itemView, viewHolder.layoutPosition)
                false
            })
        }
        bindData(viewHolder, mList!![position])
    }

    /**
     * 绑定数据
     */
    abstract fun bindData(viewHolder: RVViewHolder, bean: T)

    /**
     * 绑定动作
     */
    abstract fun bindAction(viewHolder: RVViewHolder, bean: T)

    override fun getItemCount(): Int {
        return if (mList == null) 0 else mList!!.size
    }

    /**
     * 设置点击事件
     *
     * @param mIRecyclerViewListener
     */
    fun setIRecyclerViewListener(mIRecyclerViewListener: IRecyclerViewListener) {
        this.recyclerViewListener = mIRecyclerViewListener
    }

}
